package sen.myReport;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;

/**
 * 1.选择方式：行、列、单元格
 * 2.目标格式：类css
 * @author sen
 */
public class ExcelStyle {

	private int[] col;
	private int[] row;
	/**
	 * "1,1;0,0"
	 */
	private String points;
	private String css;
	
	public ExcelStyle(int[] col, int[] row, String style) {
		this.col = col;
		this.row = row;
		this.css = style;
	}
	
	public ExcelStyle(String points, String css) {
		super();
		this.points = points;
		this.css = css;
	}


	private HSSFWorkbook wb = null;
	private HSSFCellStyle cellStyle;
	/**
	 * 设置Cell style
	 */
	public void set(Cell c) {
		if(null!=css && "".equals(css)) {
			return;
		}
		Font font = wb.createFont();
		String[] tmp = css.split(",");
		for (int i = 0; i < tmp.length; i++) {
			String[] its =  tmp[i].split(":");
			String name = its[0];
			String val = its[1];
		    
			if("color".equals(name)) {
				setColor(font, val);
			} else if("font".equals(name)) {
				font.setFontName(val);
			} else if("size".equals(name)) {
				short v = 10;
				try {
					v = Short.parseShort(val);
				} catch (NumberFormatException e) {
				}
				font.setFontHeightInPoints((short)v);
			} else if("font-weight".equals(name)) {
				short v = 700;
				if("bold".equals(val)) {
					v = 700;
				}
				try {
					v = Short.parseShort(val);
				} catch (NumberFormatException e) {
				}
				font.setBoldweight(v);
			} else if("italic".equals(name)) {
				if("true".equals(val)) {
					font.setItalic(true);
				} else {
					font.setItalic(false);
				}
			}
		}
		if(cellStyle == null) {
			cellStyle=wb.createCellStyle();
			cellStyle.setFont(font);
		}
	    c.setCellStyle(cellStyle);
	}
	/**
	 * 设置指定的格式
	 * @param cell
	 * @param i
	 * @param j
	 */
	public void setStyle(Cell cell) {
		int i = cell.getRowIndex();
		int j = cell.getColumnIndex();
		//列格式
		if(null != col) {
			for (int m = 0; m < col.length; m++) {
				if(col[m] == j) {
					set(cell);
				}
			}
		}
		//行格式
		if(null != row) {
			for (int m = 0; m < row.length; m++) {
				if(row[m] == i) {
					set(cell);
				}
			}
		}
		//单元格格式
		if(points != null && !"".equals(points)) {
			String[] tmp = points.split(";");//"1,1;0,0"
			for (int k = 0; k < tmp.length; k++) {
				String[] s = tmp[k].split(",");
				int a = Integer.parseInt(s[0]);
				int b = Integer.parseInt(s[1]);
				if(i==a && j==b) {
					set(cell);
				}
			}
		}
	}
	/**
	 * 给Font设置自定义(RGB)颜色
	 * @param font
	 * @param str
	 */
	private void setColor(Font font, String str) {
	    //处理把它转换成十六进制并放入一个数
	    int[] color=new int[3];
	    color[0]=Integer.parseInt(str.substring(1, 3), 16);
	    color[1]=Integer.parseInt(str.substring(3, 5), 16);
	    color[2]=Integer.parseInt(str.substring(5, 7), 16);
	    //自定义颜色
	    HSSFPalette palette = wb.getCustomPalette();
	    palette.setColorAtIndex(HSSFColor.BLACK.index,(byte)color[0], (byte)color[1], (byte)color[2]);
	    //将自定义的颜色引入进来
	    font.setColor(HSSFColor.BLACK.index);
	}
	public void setWb(HSSFWorkbook wb) {
		this.wb = wb;
	}
}
